from sqlalchemy import Column, BigInteger, String, SmallInteger, Integer, DateTime, Index
from app.config.database import Base
from datetime import datetime
from pydantic import BaseModel, ConfigDict
from typing import Optional

class Classroom(Base):
    __tablename__ = "edu_classroom"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment="教室ID")
    classroom_code = Column(String(10), nullable=False, unique=True, comment="教室编码（如：10101）")
    classroom_name = Column(String(50), nullable=False, comment="教室名称")
    building_no = Column(SmallInteger, nullable=False, comment="教学楼编号：1-主教学楼")
    floor_no = Column(SmallInteger, nullable=False, comment="楼层：1-4层")
    room_no = Column(SmallInteger, nullable=False, comment="教室编号：1-20")
    capacity = Column(Integer, nullable=False, comment="容纳人数")
    room_type = Column(SmallInteger, nullable=False, comment="教室类型：1-普通教室 2-实验室 3-多媒体教室 4-音乐教室 5-美术教室")
    status = Column(SmallInteger, nullable=False, default=1, comment="状态：0-停用 1-启用")
    create_time = Column(DateTime, default=datetime.utcnow, nullable=False, comment="创建时间")
    update_time = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, comment="更新时间")
    
    # 索引
    __table_args__ = (
        Index('uk_classroom_code', 'classroom_code'),
        Index('idx_building_floor', 'building_no', 'floor_no')
    )

class ClassroomInDB(BaseModel):
    id: int
    classroom_code: str
    classroom_name: str
    building_no: int
    floor_no: int
    room_no: int
    capacity: int
    room_type: int
    status: int = 1
    create_time: Optional[datetime] = None
    update_time: Optional[datetime] = None

    model_config = ConfigDict(from_attributes=True)